<template>
  <div class="login_container">
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <el-form
          class="login_form"
          :model="loginForm"
          :rules="rules"
          ref="loginForms"
        >
          <h1>hello</h1>
          <h2>欢迎来到硅谷甄选</h2>
          <el-form-item prop="username">
            <el-input
              v-model="loginForm.username"
              :prefix-icon="User"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              type="password"
              :prefix-icon="Lock"
              show-password
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              :loading="vloading"
              @click="login"
              class="login_btn"
              type="primary"
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { User, Lock } from '@element-plus/icons-vue';
import { reactive, ref } from 'vue';
// 获取路由
import { useRouter } from 'vue-router';
let $router = useRouter();
// 用户相关的pinia仓库
import useUserStore from '@/store/modules/user';
let userStore = useUserStore();
import { ElNotification } from 'element-plus';
import { getTime } from '@/utils/time';

// @ts-ignore 忽略rule未使用报错
const validatorUserName = (rule: any, value: any, callback: any) => {
  // rule：校验对象
  // value：表单元素文本内容
  // callback：如果符合条件callback放行通过，如果不符合，注入错误提示信息
  if (value.length >= 5) {
    callback();
  } else {
    callback(new Error('账号长度至少五位'));
  }
};
// @ts-ignore 忽略rule未使用报错
const validatorPassWord = (rule: any, value: any, callback: any) => {
  if (value.length >= 6) {
    callback();
  } else {
    callback(new Error('密码长度至少六位'));
  }
};

// 表单校验
const rules = {
  username: [
    // {required:true,min:6,max:10,message:"帐号长度至少6位最多10位",trigger:'change'}
    { trigger: 'change', validator: validatorUserName },
  ],
  password: [
    // {required:true,min:6,max:15,message:"密码长度至少6位最多15位",trigger:'change'}
    { trigger: 'change', validator: validatorPassWord },
  ],
};
let loginForms = ref();
let loginForm = reactive({
  username: 'admin',
  password: '111111',
});
let vloading = ref(false);
// 登录回调
const login = async () => {
  // 表单校验通过才发请求
  await loginForms.value.validate();
  vloading.value = true;
  // 也可以用.then,.catch的写法
  try {
    await userStore.userLogin(loginForm);
    $router.push('/');
    ElNotification({
      type: 'success',
      message: '欢迎回来',
      title: `Hi,${getTime()}好`,
    });
  } catch (error) {
    ElNotification({
      type: 'error',
      message: (error as Error).message,
    });
  } finally {
    vloading.value = false;
  }
};
</script>

<style lang="scss" scoped>
.login_container {
  width: 100%;
  height: 100vh;
  background: url('@/assets/images/background.jpg') no-repeat;
  background-size: cover;

  .login_form {
    position: relative;
    width: 80%;
    top: 30vh;
    background: url('@/assets/images/login_form.png') no-repeat;
    background-size: cover;
    padding: 40px;

    h1 {
      color: white;
      font-size: 40px;
    }
    h2 {
      color: white;
      font-size: 20px;
      margin: 20px 0;
    }
    .login_btn {
      width: 100%;
    }
  }
}
</style>
